import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HairShellService } from '../hair-shell.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-hair-shell-list',
  templateUrl: './hair-shell-list.component.html',
  styleUrls: ['./hair-shell-list.component.scss'],
  providers: [DatePipe]
})
export class HairShellListComponent implements OnInit {

  seachParams: any = {
    state: '',
    createTime: '',
    endTime: ''
  };
  dateArrs: Date[] = [];

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any = [];

  constructor(
    private router: Router,
    private message: NzMessageService,
    private datePipe: DatePipe,
    private services: HairShellService
  ) { }

  ngOnInit(): void {
    this.loadTable();
  }

  async loadTable() {
    try {
      let paramas = {
        ...this.seachParams,
        page: this.tableParams.page,
        pageSize: this.tableParams.pageSize
      }
      this.tableParams.tableLoading = true;

      const res: any = await this.services.getPhoneShellList(paramas);
      this.tableParams.tableLoading = false;
      if (res.code == 0) {
        const { records, total }: any = res.data;
        this.tableData = records;
        this.tableParams.total = total;
      } else {
        this.message.error(res.message || '列表数据获取失败');
      }
    } catch (error: any) {
      this.tableParams.tableLoading = false;
      this.tableData = [];
      this.tableParams.total = 0;
      this.message.error(error);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData() {
    for (let key in this.seachParams) {
      if (this.seachParams[key] = '') {
        delete this.seachParams[key];
      }
    }
    this.dateArrs = [];

    this.tableParams.page = 1;
    this.loadTable();
  }

  // 日期选择
  timeChange(result: any) {
    console.log('date arrs', result);
    this.seachParams.createTime = result.length ? this.datePipe.transform(new Date(result[0]), 'yyyy-MM-dd') : '';
    this.seachParams.endTime = result.length ? this.datePipe.transform(new Date(result[1]), 'yyyy-MM-dd') : '';
  }

  // 页面跳转（查看）
  jumpPage(id: number) {
    this.router.navigate(['/customization/hair-shell-details'], {
      queryParams: {
        id: id
      }
    })
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
